﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Text;

namespace BLL
{
    [Serializable]
    public class Common
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public string mMensaje;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private static string mConnDB;

        public string Mensaje
        {
            get { return mMensaje; }
            set { mMensaje = value; }
        }

        protected static string ConnDB
        {
            get 
            {
                if (string.IsNullOrEmpty(mConnDB))
                {
                    var tmpConexionUsada = ConfigurationManager.AppSettings["ConexionUsada"];
                    if (!string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[tmpConexionUsada].ConnectionString))
                        mConnDB = ConfigurationManager.ConnectionStrings[tmpConexionUsada].ConnectionString;
                    
                }

                return mConnDB;
            }
        }

        protected static string Encrypt(string valor)
        {
            if (valor == null) return null;

            try
            {
                byte[] encData_byte;
                encData_byte = Encoding.UTF8.GetBytes(valor);
                string encodedData = Convert.ToBase64String(encData_byte);

                return encodedData;
            }
            catch (Exception ex)
            {
                throw new Exception("Error Encrypt: " + ex.Message);
            }
        }

        protected static string Decrypt(string valorEncriptado)
        {
            if (valorEncriptado == null) return null;

            var encoder = new UTF8Encoding();
            Decoder utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte = Convert.FromBase64String(valorEncriptado);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            var decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            var result = new String(decoded_char);
            return result;
        }
    }
}
